-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
v3: revert ":sparkles: v3 (feature): use any as default Message type of Error struct (#1925)" #2000
Conversation
What do you think about as follows: type ErrorMine struct {
Code int `json:"code"`
Message string `json:"message"`
Data []any `json:"data"`
}
func (e *ErrorMine) Error() string {
if e.Data == nil {
return e.Message
}
return fmt.Sprint(e.Message, " ", e.Data)
}
func NewErrorMine(code int, message string, data ...any) *ErrorMine {
e := &ErrorMine{
Code: code,
Message: message,
Data: data,
}
return e
} In my tests, if no data is passed, the performance is the same as the original design. If data is passed the performance drops severely, even 3.5 times as much as the new design. With the flame chart I can see that the performance consumption is in the fmt.Sprint function. Moreover, maybe zap which is a log libary can provide some useful ideas. Like this API: logger.Info("failed to fetch URL",
// Structured context as strongly typed Field values.
zap.String("url", url),
zap.Int("attempt", 3),
zap.Duration("backoff", time.Second),
) |
I'd like to suggest another thing (Although your want a performance improvement suggestion, that pr will affect performance…) And IMO users can use a more complex error implement and write their own global error handler if they need to use error as HTTP error response (this is currently I'm doing). A simple built-in fiber Error with code and message is enough, no need to add a data filed. |
@ReneWerner87 I agree with you and I do the same as you said in my projects. It's just that the code being revert has this design, so I discussed a possibility. |
It was good idea to make Message type interface{} to improve Fiber error handling. However, it makes much allocations and it's big problem.
If you have better idea to make Fiber error handling better by protecting Fiber performance, please share it 💯